package cn.iocoder.yudao.module.blog.service.improvechapter;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.improvechapter.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvechapter.BlogImproveChapterDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.improvechapter.BlogImproveChapterConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.improvechapter.BlogImproveChapterMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 课程章节 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogImproveChapterServiceImpl implements BlogImproveChapterService {

    @Resource
    private BlogImproveChapterMapper improveChapterMapper;

    @Override
    public Long createImproveChapter(BlogImproveChapterCreateReqVO createReqVO) {
        // 插入
        BlogImproveChapterDO improveChapter = BlogImproveChapterConvert.INSTANCE.convert(createReqVO);
        improveChapterMapper.insert(improveChapter);
        // 返回
        return improveChapter.getId();
    }

    @Override
    public void updateImproveChapter(BlogImproveChapterUpdateReqVO updateReqVO) {
        // 校验存在
        validateImproveChapterExists(updateReqVO.getId());
        // 更新
        BlogImproveChapterDO updateObj = BlogImproveChapterConvert.INSTANCE.convert(updateReqVO);
        improveChapterMapper.updateById(updateObj);
    }

    @Override
    public void deleteImproveChapter(Long id) {
        // 校验存在
        validateImproveChapterExists(id);
        // 删除
        improveChapterMapper.deleteById(id);
    }

    private void validateImproveChapterExists(Long id) {
        if (improveChapterMapper.selectById(id) == null) {
            throw exception(IMPROVE_CHAPTER_NOT_EXISTS);
        }
    }

    @Override
    public BlogImproveChapterDO getImproveChapter(Long id) {
        return improveChapterMapper.selectById(id);
    }

    @Override
    public List<BlogImproveChapterDO> getImproveChapterByImproveId(Long improveId){
        return improveChapterMapper.selectList(BlogImproveChapterDO::getImproveId, improveId);
    }

    public List<BlogImproveChapterDO> getImproveChapterByImproveIds(Collection<Long> improveIds){
        return improveChapterMapper.selectList(BlogImproveChapterDO::getImproveId, improveIds);
    }

    @Override
    public List<BlogImproveChapterDO> getImproveChapterList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return improveChapterMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogImproveChapterDO> getImproveChapterPage(BlogImproveChapterPageReqVO pageReqVO) {
        return improveChapterMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogImproveChapterDO> getImproveChapterList(BlogImproveChapterExportReqVO exportReqVO) {
        return improveChapterMapper.selectList(exportReqVO);
    }

}
